package com.elitel.frame.base.service.impl;

import com.elitel.common.utils.UUIDUtil;
import com.elitel.frame.base.service.AcServiceFilterFieldService;
import com.elitel.frame.base.service.SysPrjServiceService;
import com.elitel.frame.main.dao.AcServiceFilterFieldMapper;
import com.elitel.frame.main.dao.ext.ExtBaseShowfieldMapper;
import com.elitel.frame.main.entity.AcServiceFilterField;
import com.elitel.frame.main.entity.SysPrjservice;
import com.elitel.frame.main.entity.enums.FilterTypeEnum;
import com.elitel.frame.main.entity.ext.BaseShowfieldExt;
import com.elitel.frame.main.entity.vo.AcServiceFilterFieldFormVo;
import com.elitel.frame.main.entity.vo.AcServiceFilterFieldVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AcServiceFilterFieldServiceImpl implements AcServiceFilterFieldService {
    private static final String ADMIN ="admin";
    @Autowired
    private ExtBaseShowfieldMapper extBaseShowfieldMapper;

    @Autowired
    private AcServiceFilterFieldMapper acServiceFilterFieldMapper;
    @Autowired
    SysPrjServiceService sysPrjServiceService;

    @Override
    public List<AcServiceFilterFieldVo> getDataList(String serviceCode) {
        List<AcServiceFilterFieldVo> resultList  = new ArrayList<>();
        List<BaseShowfieldExt> baseShowFieldExtList = extBaseShowfieldMapper.selectAllFieldWithCode(serviceCode);
        SysPrjservice sysPrjservice = sysPrjServiceService.getSysPrjserviceByServiceCode(serviceCode);
        if (sysPrjservice == null) {
            return resultList;
        }
        List<AcServiceFilterField> acServiceFilterFieldList = acServiceFilterFieldMapper.selectByServiceId(sysPrjservice.getGuid());
        baseShowFieldExtList.forEach(baseShowFieldExt -> {
            AcServiceFilterFieldVo acServiceFilterFieldVo = new AcServiceFilterFieldVo();
            acServiceFilterFieldVo.setFieldId(baseShowFieldExt.getGuid());
            acServiceFilterFieldVo.setServiceId(baseShowFieldExt.getServiceguid());
            acServiceFilterFieldVo.setShoworder(baseShowFieldExt.getShoworder());
            acServiceFilterFieldVo.setFieldname(baseShowFieldExt.getFieldname());
            acServiceFilterFieldVo.setFieldnameCn(baseShowFieldExt.getFieldnameCn());
            acServiceFilterFieldVo.setFieldtype(baseShowFieldExt.getFieldtype());
            acServiceFilterFieldVo.setFilterStationFlag(FilterFlag.NO.name());
            acServiceFilterFieldVo.setFilterTimeFlag(FilterFlag.NO.name());
            resultList.add(acServiceFilterFieldVo);
            acServiceFilterFieldList.forEach(acServiceFilterField -> {
                if (baseShowFieldExt.getFieldname().equals(acServiceFilterField.getFieldCode())) {
                    if (FilterTypeEnum.STCD.name().equals(acServiceFilterField.getFilterType())) {
                        acServiceFilterFieldVo.setFilterStationFlag(FilterFlag.YES.name());
                    } else if (FilterTypeEnum.TM.name().equals(acServiceFilterField.getFilterType())) {
                        acServiceFilterFieldVo.setFilterTimeFlag(FilterFlag.YES.name());
                    }
                }
            });
        });
        return resultList;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insert(AcServiceFilterFieldFormVo acServiceFilterFieldVo) {
        //1.删除已存在的记录
        int result = acServiceFilterFieldMapper.deleteByServiceId(acServiceFilterFieldVo.getServiceId());
        //2.插入新记录
        List<AcServiceFilterField> acServiceFilterFieldList = acServiceFilterFieldVo.getAcServiceFilterFieldList();
        AcServiceFilterField acServiceFilterField;
        for (int i = 0; i < acServiceFilterFieldList.size(); i++) {
            acServiceFilterField = acServiceFilterFieldList.get(i);
            if (ObjectUtils.isEmpty(acServiceFilterField.getFilterFieldId())) {
                acServiceFilterField.setFilterFieldId(UUIDUtil.randomUUID());
                acServiceFilterField.setCreatedTime(new Date());
                acServiceFilterField.setCreatedUser(ADMIN);
                acServiceFilterField.setCreatedTime(new Date());
            }
            acServiceFilterField.setUpdatedUser(ADMIN);
            acServiceFilterField.setUpdatedTime(new Date());
            result += acServiceFilterFieldMapper.insert(acServiceFilterFieldList.get(i));
        }
        return result;
    }

    enum FilterFlag {
        /**
         * 是
         */
        YES,
        /**
         * 否
         */
        NO
    }
}
